﻿package cn.edu.zjusc.Security;

import java.util.HashMap;
import java.util.UUID;

public class Authentication {

	private static HashMap<String, DepositSlot> userInfoHash = new HashMap<String, DepositSlot>();

	/**
	 * 添加用户登陆
	 * 
	 * @param userName
	 * @return 权限码
	 */
	public static String addUserLogin(String userName, int userType) {

		UUID uuid = UUID.randomUUID();
		// 生成Token
		String genedCode = uuid.toString().replace("-", "");

		DepositSlot dslot = userInfoHash.get(userName);
		if (dslot != null) {
			// 如果链表里有，就更新Token
			dslot.setAuthCode(genedCode);
			dslot.setUserType(userType);
		} else {
			// 如果链表里没有
			dslot = new DepositSlot();
			dslot.setUserName(userName);
			dslot.setAuthCode(genedCode);
			dslot.setUserType(userType);
			userInfoHash.put(userName, dslot);
		}

		System.out.println("用户 " + userName + " 验证码 " + genedCode);
		return genedCode;
	}

	/**
	 * 校验符合用户最低门槛权限
	 * 
	 * @param uc
	 * @return
	 */
	public static boolean checkUserValidation(UserCave uc) {

		DepositSlot dsCheck = userInfoHash.get(uc.getUserName());
		if (dsCheck==null ){
			return false;
		}
		else if (dsCheck.getAuthCode().equals(uc.getAuthCode())) {
			return true;
		}
		return false;
	}

	/**
	 * 校验老师权限
	 * 
	 * @param uc
	 * @return
	 */
	public static boolean checkIsTeacher(UserCave uc) {

		DepositSlot dsCheck = userInfoHash.get(uc.getUserName());
		if (dsCheck==null ){
			return false;
		}
		else if ( dsCheck.getUserType() == 100) {
			return true;
		}
		return false;
	}

	/**
	 * 校验学生权限
	 * 
	 * @param uc
	 * @return
	 */
	public static boolean checkIsStudent(UserCave uc) {

		DepositSlot dsCheck = userInfoHash.get(uc.getUserName());
		if (dsCheck==null ){
			return false;
		}
		else if ( dsCheck.getUserType() >= 1) {
			return true;
		}
		return false;
	}

}
